package it.itsvil.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

public class UploadFile {

	private static final String TMP_DIR_PATH = "/opt/jboss-5.1.0.GA/tmp";
	private File tmpDir;
	private static final String DESTINATION_DIR_PATH = "/opt/jboss-5.1.0.GA/upload";
	private File destinationDir;

	public File upload(HttpServletRequest request) {
		String saveFile = "";
		tmpDir = new File(TMP_DIR_PATH);
		destinationDir = new File(DESTINATION_DIR_PATH);

		try {
			if (!tmpDir.isDirectory())
				throw new ServletException(TMP_DIR_PATH + " is not a directory");
			if (!destinationDir.isDirectory())
				throw new ServletException(DESTINATION_DIR_PATH
						+ " is not a directory");
			// to get the content type information from JSP Request Header
			String contentType = request.getContentType();
			System.out.println("Content type is: " + contentType);
			// here we are checking the content type is not equal to Null and as
			// well as the passed data from mulitpart/form-data is greater than
			// or equal to 0
			if ((contentType != null)
					&& (contentType.indexOf("multipart/form-data") >= 0)) {
				DataInputStream in = new DataInputStream(
						request.getInputStream());
				// we are taking the length of Content type data
				int formDataLength = request.getContentLength();
				byte dataBytes[] = new byte[formDataLength];
				int byteRead = 0;
				int totalBytesRead = 0;
				// this loop converting the uploaded file into byte code
				while (totalBytesRead < formDataLength) {
					byteRead = in.read(dataBytes, totalBytesRead,
							formDataLength);
					totalBytesRead += byteRead;
				}
				String file = new String(dataBytes);
				// for saving the file name
				saveFile = file.substring(file.indexOf("filename=\"") + 10);
				System.out.println("s" + saveFile);
				saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
				System.out.println(saveFile);
				saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
						saveFile.indexOf("\""));
				System.out.println(saveFile);
				int lastIndex = contentType.lastIndexOf("=");
				String boundary = contentType.substring(lastIndex + 1,
						contentType.length());
				int pos;
				// extracting the index of file
				pos = file.indexOf("filename=\"");
				pos = file.indexOf("\n", pos) + 1;
				pos = file.indexOf("\n", pos) + 1;
				pos = file.indexOf("\n", pos) + 1;
				int boundaryLocation = file.indexOf(boundary, pos) - 4;
				int startPos = ((file.substring(0, pos)).getBytes()).length;
				int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
				// creating a new file with the same name and writing the
				// content in new file
				FileOutputStream fileOut = new FileOutputStream(destinationDir
						+ saveFile);
				fileOut.write(dataBytes, startPos, (endPos - startPos));
				fileOut.flush();
				fileOut.close();
			}
		} catch (IOException | ServletException e) {
		}
		System.out.println("File saved: " + saveFile);
		return new File(destinationDir + saveFile);
	}
}
